package com.chuiyan.topnews;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.Volley;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class FragmentDemo extends Fragment {

    private TextView textView;
    private String text = "默认文本";
    private View viewContent;
    private LinearLayout linearLayout;
    private RecyclerView recyclerView;
    private ArrayList<JSONObject> jsonObjects;
    private ImgRecyclerAdapter imgRecyclerAdapter;
    public FragmentDemo(){

    }

    /*
    * 测试类
    * */
    @SuppressLint("ValidFragment")
    public FragmentDemo(String text){
        this.text = text;
    }

    @SuppressLint("ValidFragment")
    public FragmentDemo(View view){
        this.viewContent = view;
    }


    public View onCreateView(LayoutInflater inflater , ViewGroup container, Bundle savedInstanceState){
        View view=inflater .inflate(R.layout .fragmentdemo ,container,false) ;
//        textView = view.findViewById(R.id.textViewDemo);
//        textView.setText(text);
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        linearLayout = getView().findViewById(R.id.content);
        recyclerView = getView().findViewById(R.id.list);
        linearLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(),Detail_img.class);
                startActivity(intent);
            }
        });
        jsonObjects = new ArrayList<>();
        try {
            initData();
        } catch (JSONException e) {
            e.printStackTrace();
        }
        imgRecyclerAdapter = new ImgRecyclerAdapter(getContext(),jsonObjects);
        recyclerView.setAdapter(imgRecyclerAdapter);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext(),LinearLayoutManager.VERTICAL,false));
        DividerLine dividerLine = new DividerLine(DividerLine.VERTICAL);
        dividerLine.setSize(2);
        dividerLine.setColor(0xFFDDDDDD);
        recyclerView.addItemDecoration(dividerLine);
        imgRecyclerAdapter.setOnItemClickListener(new ImgRecyclerAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, JSONObject jsonObject) {
                Intent intent = new Intent(getActivity(),Detail_img.class);
                startActivity(intent);
            }
        });
    }

    public void initData() throws JSONException {
        for(int i=0;i<10;i++)
        {
            //获取list数据
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("title","习近平：贯彻新发展理念推动高质量发展，奋力开创中部地区崛起新局面");
            jsonObject.put("image",1);
            jsonObjects.add(jsonObject);
        }
    }

    /*
     * 后端请求模板
     * */

    public void initDatatemp() throws JSONException {
        Map<String, String> map = new HashMap<>();
        map.put("type","1");
        if(jsonObjects.size()==0)
        {
            //1.创建请求队列
            InitQueue.queue = Volley.newRequestQueue(getContext());
            //2.创建post请求
            CustomRequest jsonObjectRequest = new CustomRequest(Request.Method.POST, "http://10.0.2.2:8080/news", map, new Response.Listener<JSONObject>() {

                @Override
                public void onResponse(JSONObject response) {
                    //Toast.makeText(getContext(), "成功了", Toast.LENGTH_SHORT).show();
                    //System.out.println(response);
                    JSONArray jsonArray = new JSONArray();
                    try {
                        jsonArray = response.getJSONArray("newsList");
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    for(int i=0;i<jsonArray.length();i++)
                    {
                        try {
                            jsonObjects.add(jsonArray.getJSONObject(i));
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }

            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError volleyError) {
                    Toast.makeText(getContext(), "失败了", Toast.LENGTH_SHORT).show();
                }
            });
            //3.添加请求队列
            jsonObjectRequest.setTag("get_list");
            InitQueue.getHttpQueue().add(jsonObjectRequest);
        }

    }

}
